package lg.minecraft.plugin.pokemonmenu.gui.edit;

import com.cobblemon.mod.common.api.pokeball.PokeBalls;
import com.cobblemon.mod.common.pokeball.PokeBall;
import com.cobblemon.mod.common.pokemon.Pokemon;
import lg.minecraft.plugin.cobblemoncore.util.BukkitNmsConverter;
import lg.minecraft.plugin.library.inventory.CancelInventoryContext;
import lg.minecraft.plugin.library.inventory.ClickInventoryContext;
import lg.minecraft.plugin.library.inventory.InventoryContext;
import lg.minecraft.plugin.library.inventory.LGInventory;
import lg.minecraft.plugin.pokemonmenu.LGPokemonMenu;
import lg.minecraft.plugin.pokemonmenu.gui.EditInventor;
import org.bukkit.Sound;
import org.bukkit.entity.Player;

import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
public class EditPokemonBallInventor extends LGInventory {

    private static final int[] GLASS_ARRAY = {45, 46, 47, 51, 52, 53};

    private static final List<PokeBall> pokeBalls = PokeBalls.INSTANCE.all();
    private static final int PAGE_SIZE = 45; // 每页显示的数量

    private final int totalPages = (pokeBalls.size() + PAGE_SIZE - 1) / PAGE_SIZE; // 总页数

    private final Pokemon pokemon;
    private int page = 0; // 当前页码

    public EditPokemonBallInventor(Pokemon pokemon) {
        super(6, LGPokemonMenu.getMessage().getGuiTitleEditPokeBall());
        this.pokemon = pokemon;
    }

    @Override
    public void onInventoryOpen(CancelInventoryContext context) {
        inventory.clear();

        // 显示当前页的精灵球
        int startIndex = page * PAGE_SIZE;
        int endIndex = Math.min(startIndex + PAGE_SIZE, pokeBalls.size());

        for (int i = 0; i < endIndex - startIndex; i++) {
            PokeBall pokeBall = pokeBalls.get(startIndex + i);
            inventory.setItem(i, BukkitNmsConverter.getPokeBallItem(pokeBall));
        }

        for (int slot : GLASS_ARRAY) inventory.setItem(slot, LGPokemonMenu.getGuiItem().getBorder());

        inventory.setItem(49, LGPokemonMenu.getGuiItem().getBack());
        inventory.setItem(48, LGPokemonMenu.getGuiItem().getPreviousPage(page + 1, totalPages));
        inventory.setItem(50, LGPokemonMenu.getGuiItem().getNextPage(page + 1, totalPages));
    }

    @Override
    public void onInventoryClick(ClickInventoryContext context) {
        context.setCancelled(true);
        Player player = context.getPlayer();
        int slot = context.getSlot();

        if (slot == 49) {
            new EditInventor(pokemon).openInventory(player);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        } else if (slot == 48 && page > 0) {
            page--;
            onInventoryOpen(context);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        } else if (slot == 50 && page < totalPages - 1) {
            page++;
            onInventoryOpen(context);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        } else if (slot >= 0 && slot < PAGE_SIZE) {
            // 点击了精灵球
            int index = page * PAGE_SIZE + slot;
            if (index < pokeBalls.size()) {
                PokeBall selectedBall = pokeBalls.get(index);
                pokemon.setCaughtBall(selectedBall);
                new EditInventor(pokemon).openInventory(player);
                player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1.0F, 1.8F);
            }
        }
    }

    @Override
    public void onInventoryClose(InventoryContext context) {
    }

}

